<template>
  <div class="app-container">
    <!-- <strong>添加讲师</strong> -->

    <el-form ref="superForm" :model="superAll" :rules="rules" label-width="120px">

      <el-form-item label="审核员姓名" prop="name" style="width: 350px;">
        <el-input v-model="superAll.name" clearable placeholder="请如实填写"></el-input>
      </el-form-item>

      <el-form-item label="审核员工号" prop="adminUsername" style="width: 450px;">
        <el-input v-model="superAll.adminUsername" clearable placeholder="请输入正确格式的教职工号"></el-input>
      </el-form-item>

      <el-form-item label="所属学院" prop="supCollege">
        <el-select v-model="superAll.supCollege" clearable placeholder="所属专业学院">
          <!-- 选项内容 -->
          <el-option :value="1" label="商学院"/>
          <el-option :value="2" label="管理学院"/>
          <el-option :value="3" label="外语学院"/>
          <el-option :value="4" label="堪萨斯国际学院"/>
          <el-option :value="5" label="电信与智能制造学院"/>
          <el-option :value="6" label="计算机与软件工程学院"/>
          <el-option :value="7" label="艺术设计学院"/>
          <el-option :value="8" label="医学院"/>
          <el-option :value="9" label="文法学院(语委办)"/>
          <el-option :value="10" label="体育学院"/>
          <el-option :value="11" label="新闻与传播学院"/>
          <el-option :value="12" label="建筑工程学院"/>
          <el-option :value="13" label="音乐戏剧学院"/>
          <el-option :value="14" label="马克思主义学院"/>
          <el-option :value="15" label="国际教育学院"/>
          <el-option :value="16" label="教育学院"/>
          <el-option :value="17" label="继续教育学院"/>
          <el-option :value="18" label="国际语言中心"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="saveOrUpdate()">提交</el-button>
      </el-form-item>

      <!-- TODO -->

    </el-form>
  </div>
</template>

<script>
// 假设已经导入了API
import * as teacher from '@/api/adm/teacherAdmin'

export default {
  data() {
    return {
      superAll:{
        adminUsername:'',
        supCollege:null,
        name:''
      },
      rules: {
        name: [{ required: true, message: '请输入审核员姓名', trigger: 'blur' }],
        adminUsername: [{ required: true, message: '请输入审核员工号', trigger: 'blur' }],
        supCollege: [{ required: true, message: '请选择所属学院', trigger: 'change' }]
      }
    }
  },
  created() {
    this.init()
  },
  watch: {
    $route(to, from) {
      this.init()
    }
  },
  methods: {
    init() {
      if(this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.getInfo(id)
      } else {
        this.superAll = {adminUsername:'', supCollege:null, name:''}
      }
    },
    saveOrUpdate(){
      this.$refs.superForm.validate((valid) => {
        if (valid) {
          if(!this.$route.params.id) {
            this.saveSuper()
          } else {
            this.updateSuper()
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //添加教师信息
    saveSuper(){
      teacher.addSuper(this.superAll).then(response => {
        //添加成功
        this.$message({
          message: "添加成功",
          type: 'success'
        });
        this.teacher = null;
        this.$router.push({path:'/adminSuper/list'});
      }).catch(error => {
        // 请求失败
        console.error(error);
      });
    },
    //修改教师信息前的回显
    getInfo(id){
      teacher.getInfoSuper(id).then(response => {
        //回显成功
        this.superAll = response.data.admSuperadministrator
      })
    },
    //修改教师信息
    updateSuper(){
      teacher.updateSuper(this.superAll,this.$route.params.id).then(response => {
        //修改成功
        //添加成功
        this.$message({
          message: "修改成功",
          type: 'success'
        });
        // this.teacher = null;
        this.$router.push({path:'/adminSuper/list'});
      }).catch(error => {
        // 请求失败
        console.error("请求有误，请联系工作人员！");
      });
    }
  }
}
</script>
